﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;// Permitir conexão com Access.
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using System.Configuration;
using System.Security.Cryptography;




namespace SistemaPetShop
{
    public class Conexao
    {

        
        EventLog log = new EventLog();




        public static string con = System.Configuration.ConfigurationManager.ConnectionStrings["MySql"].ToString();

        public static string senhaMySql = descriptografaSenha(con.ToString(), "mysql");

        MySqlConnection conexao = new MySqlConnection(senhaMySql);


        //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\PETSHOP\\sistema\\PetShop\\SistemaPetShop\\SistemaPetShop\\BaseCorreio\\dnecom.mdb;

        public static string conAccess = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ToString();

        public static string senhaAccess = descriptografaSenha(conAccess.ToString(), "access");

        OleDbConnection conexaoAccess = new OleDbConnection(senhaAccess);
        

            
            //this.descriptografaSenha(System.Configuration.ConfigurationManager.ConnectionStrings["MySql"].ToString(),"mysql"));



        public MySqlConnection ConexaoMySql
        {
            get { return conexao; }
            set { conexao = value; }
        }


        public void AbrirConexaoAccess()
        {
            try
            {
                conexaoAccess.Open();
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                throw;
            }
        }

        public OleDbDataReader ConsultaAccess(string comando)
        {
            try
            {
                DataTable dt = new DataTable();
                OleDbCommand c = new OleDbCommand(comando, conexaoAccess);
                OleDbDataReader Consulta = c.ExecuteReader();
                return Consulta;
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                throw;
            }

        }

        public void FecharConexaoAccess()
        {
            try
            {
                conexaoAccess.Close();
                //conexaoAccess.Dispose();
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }





        public void AbrirConexao()
        {
            try
            {

                conexao.Open();
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }

        public void FecharConexao()
        {
            try
            {
                conexao.Close();
                conexao.Dispose();
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }


        public bool Insert(string comando)
        {
            try
            {
                int ret;

                //AbrirConexao(); // AAD
                MySqlCommand cmdtext = new MySqlCommand(comando, conexao);
                ret = cmdtext.ExecuteNonQuery();
                //FecharConexao(); // AAD
                if (ret >= 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                return false;
            }


        }

        public MySqlDataReader Consulta(string comando)
        {
            try
            {
                //AbrirConexao();
                MySqlCommand c = new MySqlCommand(comando,conexao);

                MySqlDataReader Consulta = c.ExecuteReader();
                //FecharConexao();
                return Consulta;
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                throw;
            }

        }

        public String _ConsultaString(string comando)
        {
            try
            {
                String ret;
                MySqlCommand c = new MySqlCommand(comando, conexao);
                MySqlDataReader Consulta = c.ExecuteReader();
                Consulta.Read();
                ret = Consulta[0].ToString();
                return ret;
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                throw;
            }

        }



        public static string criptografaSenha(string senhaCripto, string chave)
        {
            try
            {
                TripleDESCryptoServiceProvider objcriptografaSenha = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objcriptoMd5 = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;
                string strTempKey = chave;

                byteHash = objcriptoMd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                objcriptoMd5 = null;
                objcriptografaSenha.Key = byteHash;
                objcriptografaSenha.Mode = CipherMode.ECB;

                byteBuff = ASCIIEncoding.ASCII.GetBytes(senhaCripto);
                return Convert.ToBase64String(objcriptografaSenha.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                return "Digite os valores Corretamente." + ex.Message;
            }
        }


        public static string descriptografaSenha(string strCriptografada, string chave)
        {
            try
            {
                TripleDESCryptoServiceProvider objdescriptografaSenha = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objcriptoMd5 = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;
                string strTempKey = chave;

                byteHash = objcriptoMd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                objcriptoMd5 = null;
                objdescriptografaSenha.Key = byteHash;
                objdescriptografaSenha.Mode = CipherMode.ECB;

                byteBuff = Convert.FromBase64String(strCriptografada);
                string strDecrypted = ASCIIEncoding.ASCII.GetString(objdescriptografaSenha.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
                objdescriptografaSenha = null;

                return strDecrypted;
            }
            catch (Exception ex)
            {
                return "Digite os valores Corretamente." + ex.Message;
            }
        }

    }
}
